/*
 * Copyright (c) Thorben Linneweber and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using Jitter2.LinearMath;

using SoftFloat;

namespace Jitter2.Collision.Shapes
{
	
	/// <summary>
	/// Represents a shape in the form of a capsule.
	/// </summary>
	public class CapsuleShape : RigidBodyShape
	{
	    private sfloat radius;
	    private sfloat halfLength;
	
	    /// <summary>
	    /// Gets or sets the radius of the capsule.
	    /// </summary>
	    public sfloat Radius
	    {
	        get => radius;
	        set
	        {
	            radius = value;
	            UpdateWorldBoundingBox(sfloat.Zero);
	        }
	    }
	
	    /// <summary>
	    /// Gets or sets the length of the cylindrical part of the capsule, excluding the half-spheres on both ends.
	    /// </summary>
	    public sfloat Length
	    {
	        get => (sfloat)2.0f * halfLength;
	        set
	        {
	            halfLength = value / (sfloat)2.0f;
	            UpdateWorldBoundingBox(sfloat.Zero);
	        }
	    }
	
	    /// <summary>
	    /// Initializes a new instance of the CapsuleShape class with the specified radius and length. The symmetry axis of the capsule is aligned along the Y-axis.
	    /// </summary>
	    /// <param name="radius">The radius of the capsule.</param>
	    /// <param name="length">The length of the cylindrical part of the capsule, excluding the half-spheres at both ends.</param>
	    public CapsuleShape(sfloat radius /*= (sfloat)0.5f*/, sfloat length /*= (sfloat)1.0f*/)
	    {
	        this.radius = radius;
	        halfLength = (sfloat)0.5f * length;
	        UpdateWorldBoundingBox(sfloat.Zero);
	    }
	
	    public override void SupportMap(in NVector3 direction, out NVector3 result)
	    {
	        // capsule = segment + sphere
	
	        // sphere
	        NVector3.Normalize(direction, out NVector3 ndir);
	        result = ndir * radius;
	
	        // two endpoint of the segment are
	        // p_1 = (0, +length/2, 0)
	        // p_2 = (0, -length/2, 0)
	
	        // we have to calculate the dot-product with the direction
	        // vector to decide whether p_1 or p_2 is the correct support point
	        result.Y += libm.Sign(direction.Y) * halfLength;
	    
	        result += centerOffset;// 应用中心偏移
		}
	
	    public override void GetCenter(out NVector3 point)
	    {
	        point = centerOffset;
	    }
	
	    public override void CalculateBoundingBox(in NQuaternion orientation, in NVector3 position, out JBBox box)
	    {
	        NVector3 delta = halfLength * orientation.GetBasisY();
	
	        box.Max.X = +radius + libm.Abs(delta.X);
	        box.Max.Y = +radius + libm.Abs(delta.Y);
	        box.Max.Z = +radius + libm.Abs(delta.Z);
	
	        box.Min = -box.Max;
	
	        box.Min += position;
	        box.Max += position;
	    
	        // 考虑位置和中心偏移
	        box.Min += centerOffset;
	        box.Max += centerOffset;
		}
	
	    public override void CalculateMassInertia(out JMatrix inertia, out NVector3 com, out sfloat mass)
	    {
	        sfloat length = (sfloat)2.0f * halfLength;
	
	        sfloat massSphere = (sfloat)4.0f / (sfloat)3.0f * libm.PI * radius * radius * radius;
	        sfloat massCylinder = libm.PI * radius * radius * length;
	
	        inertia = JMatrix.Identity;
	
	        inertia.M11 = massCylinder * ((sfloat)1.0f / (sfloat)12.0f * length * length + (sfloat)1.0f / (sfloat)4.0f * radius * radius) + massSphere *
	            ((sfloat)2.0f / (sfloat)5.0f * radius * radius + (sfloat)1.0f / (sfloat)4.0f * length * length + (sfloat)3.0f / (sfloat)8.0f * length * radius);
	        inertia.M22 = (sfloat)1.0f / (sfloat)2.0f * massCylinder * radius * radius + (sfloat)2.0f / (sfloat)5.0f * massSphere * radius * radius;
	        inertia.M33 = massCylinder * ((sfloat)1.0f / (sfloat)12.0f * length * length + (sfloat)1.0f / (sfloat)4.0f * radius * radius) + massSphere *
	            ((sfloat)2.0f / (sfloat)5.0f * radius * radius + (sfloat)1.0f / (sfloat)4.0f * length * length + (sfloat)3.0f / (sfloat)8.0f * length * radius);
	
	        mass = massCylinder + massSphere;
	        com = NVector3.Zero;
	    }
	}
}
